<?

/**
 * Action class. Each user's defined action must extendeds this class.
 * 
 * @package SpotLight.core.events
 * @author asmhack
 * @version since begin
 * @license GPL
 * @copyright Serhiy Zaporozhets'
 */
abstract class Action extends Errors implements Actionable, Serviceable, Localizable, Templatable {
   
   public static $SUCCESS = "success";
   public static $NONE = "none";
   public static $ERROR = "error";
   public static $INPUT = "input";
   public static $LOGIN = "login";
   
   /**
    * true if action was invoked by ajax.
    *
    * @var unknown_type
    */
   protected $ajaxAction;
   
   /**
    * true if action was invoked by ajax.
    *
    * @var unknown_type
    */
   protected $isForward;
   
   /**
    * Name of Action
    *
    * @var unknown_type
    */
   protected $myName;
   
   /**
    * class name of Action
    *
    * @var unknown_type
    */
   protected $myClassName;
   
   protected $result;
   
   /**
    * Response class instance
    * Provides manipulation for responce object
    * @var unknown_type
    */
   protected $responce;
   
   /**
    * logger instance. in code you can call it, example: $this->log->debug('Im in action: '.$this->myName); 
    * @var Log4php
    */
   protected $log;
   
   public function __construct($className, $actionName, $isForward) {
      parent::__construct ();
      //$this->clear();
      $this->myName = $actionName;
      $this->myClassName = $className;
      $this->isForward = $isForward;
      
      $this->responce = Response::getInstance ();
      
      $this->log = Log4php::getInstance('debug.log' );
   }
   
   public function __set($name, $value) {
      #may
      if (property_exists ( $this, $name ))
         $this->$name = $value;
   }
   
   public function __get($name) {
      if (property_exists ( $this, $name ))
         return $this->$name;
   }
   
   /**
    * Default before execute() action.
    *
    */
   public function before() {
      return;
   }
   
   /**
    * Default after execute() action.
    *
    */
   public function after() {
      return;
   }
   
   /**
    * Main business logic
    *
    * @return String execution value, success, error, etc... or generated and filled template to echo or simple text to echo
    */
   public function execute() {
      return null;
   }
   /**
    * Preload main template and all important areas if the ajax action was invoked not by asynchronously.
    *
    */
   public function noAjax() {
      # load template and all areas in template (maybe .php file, use ob_start() in Templater.)
   }
   
   /**
    * Get Localized message from /i18n/<locale>.php file. Where <locale> is default locale, 
    * (set it in /SpotLight/configs/config.php) if browsers defined locale not found. or browsers defined locale.
    *
    * @param string $key key to array with messages.
    * @return string return message from /i18n/<locale>.php
    */
   final public final function getMessage($key) {
      return Localizator::getInstance()->getMessage ( $key );
   }
   
   /**
    * get action result
    * @return action result
    */
   final public function getResult() {
      return $this->result;
   }
   
   /**
    * Service method
    * @param unknown_type $result
    */
   final public function setResult($result) {
      $this->result = $result;
   }
   
   /**
    * Forward action.
    *
    * @param unknown_type $actionName
    */
   final public function forward($actionName) {
      $flow = WorkFlow::getInstance ();
      if (! $flow->isInFlow ( $actionName )) {
         new SpotLight ($actionName, true );
      }
   }
   
   final public function isForward() {
      return $this->isForward;
   }
   
   /**
    * Service method
    *
    * @param unknown_type $ajaxAction
    */
   final public function setAjaxAction($ajaxAction) {
      $this->ajaxAction = $ajaxAction;
   }
   
   /**
    * Service method
    *
    * @return true if it's action was invoked by ajax
    */
   final public function isAjaxAction() {
      return $this->ajaxAction;
   }
   
   /**
    * Service method
    * 
    * @return unknown_type
    */
   final public function getMyName() {
      return $this->myName;
   }
   
   /**
    * Service method
    * 
    * @param unknown_type $myName
    */
   final public function setMyName($myName) {
      $this->myName = $myName;
   }
   
   /**
    * Service method
    * 
    * @return unknown_type
    */
   final public function getMyClassName() {
      return $this->myClassName;
   }
   
   /**
    * Service method
    * 
    * @param unknown_type $myClassName
    */
   final public function setMyClassName($myClassName) {
      $this->myClassName = $myClassName;
   }
   
   /**
    * Templater::get delegate method.
    *
    * @param string $pathName template's file path
    * @param array $params list of params to replace in template
    * @return string formatted template.
    */
   final public function load($pathName, $params = array()) {
      return Templater::get ( $pathName, $params );
   }
   
   /**
    * Set default template in worlflow.
    *
    * @param string $pathName template's file path
    */
   final public function setTemplate($pathName) {
      Templater::setTemplatePath ( $pathName );
   }

}
?>